import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/pages/Home/components/home_list.dart';
import 'package:flutter_house_manager/pages/Home/components/home_nav.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';
import 'package:flutter_house_manager/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => __HomePagStateState();
}

class __HomePagStateState extends State<HomePage> {
  List _announceList = [];
  @override
  initState() {
    super.initState();
    getAnnounceList();
  }

  // 1.获取公告列表
  getAnnounceList() async {
    try {
      final data = await getAnnounceListAPI();
      PromptAction.showSuccess('成功');
      setState(() {
        _announceList = data;
      });
    } catch (e) {
      PromptAction.showError('失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Container(
          alignment: Alignment.center,
          width: double.infinity,
          child: const Text(
            '享+社区',
            style: TextStyle(fontSize: 20),
          ),
        ),
      ),
      body: ListView(
        children: [
          testToken(),
          // testTimer(),
          const HomeNav(),
          Image.asset(
            "assets/images/banner@2x.jpg",
            width: double.infinity,
            fit: BoxFit.cover,
          ),
          HomeList(list: _announceList),
        ],
      ),
    );
  }

// 倒计时测试
  int _count = 60;
  Timer? _timer;
  testTimer() {
    return Row(
      children: [
        ElevatedButton(
          onPressed: () {
            if (_timer != null) {
              return;
            }
            _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
              setState(() {
                _count--;
              });
            });
          },
          child: const Text('开始'),
        ),
        Text(_count.toString()),
        ElevatedButton(
          onPressed: () {
            if (_timer == null) {
              return;
            }
            _timer!.cancel();
            _timer = null;
            setState(() {
              _count = 60;
            });
          },
          child: const Text('结束'),
        ),
      ],
    );
  }

// 测试token
  testToken() {
    return Row(
      children: [
        ElevatedButton(
          onPressed: () async {
            await tokenManager.setToken('123456');
          },
          child: const Text('设置token'),
        ),
        ElevatedButton(
          onPressed: () async {
            final token = tokenManager.getToken();
            PromptAction.showSuccess(token);
          },
          child: const Text('获取token'),
        ),
        ElevatedButton(
          onPressed: () async {
            await tokenManager.removeToken();
            PromptAction.showSuccess('删除成功');
          },
          child: const Text('删除token'),
        ),
      ],
    );
  }
}
